package com.servlet;

import com.beans.MemberInfo;
import com.beans.PageInfo;
import com.dao.MemberDao;
import com.utils.PageUtil;
import com.utils.StrUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/MemberServlet.do")
public class MemberServlet extends HttpServlet {
    MemberDao memberDao=new MemberDao();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String flag=request.getParameter("flag");
        if("manage".equals(flag)){
            manage(request,response);
        }
        else if("del".equals(flag)){
            del(request,response);
        }
    }
    //删除单个会员
    private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id=Integer.parseInt(request.getParameter("id"));
        int result=memberDao.del(id);
        if(result==1){
            request.setAttribute("msg","删除成功");
            manage(request,response);
        }
    }
    //会员管理
    private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String  memberNo=StrUtil.isNullOrEmpty(request.getParameter("memberNo"))?"":request.getParameter("memberNo");
        String beginDate=StrUtil.isNullOrEmpty(request.getParameter("beginDate"))?"":request.getParameter("beginDate");
        String endDate=StrUtil.isNullOrEmpty(request.getParameter("endDate"))?"":request.getParameter("endDate");
        int pageIndex=StrUtil.isNullOrEmpty(request.getParameter("pageIndex"))?1:Integer.parseInt(request.getParameter("pageIndex"));

        int pageSize=5;
        int rowCount=memberDao.getMemberCount(memberNo,beginDate,endDate);

        PageInfo pageInfo= PageUtil.getPageInfo(pageSize, rowCount, pageIndex);
        List<MemberInfo> memberList=memberDao.getMemberList(memberNo,beginDate,endDate,pageInfo);
        request.setAttribute("memberList", memberList);
        request.setAttribute("pageInfo", pageInfo);
        request.getRequestDispatcher("/member/member_manage.jsp").forward(request, response);
    }
}
